﻿@page "/identity/users"
@attribute [Authorize(IdentityPermissions.Users.Default)]
@using Microsoft.AspNetCore.Authorization
@using Volo.Abp.PermissionManagement.Blazor.Components
@using Volo.Abp.BlazoriseUI.Components.ObjectExtending
@using Volo.Abp.Identity.Localization
@using Volo.Abp.AspNetCore.Components.Web.Theming.Layout
@inject AbpBlazorMessageLocalizerHelper<IdentityResource> LH

@inherits AbpCrudPageBase<IIdentityUserAppService, IdentityUserDto, Guid, GetIdentityUsersInput, IdentityUserCreateDto, IdentityUserUpdateDto>

<Card>
    <CardHeader>
        @* ************************* PAGE HEADER ************************* *@
        <PageHeader Title="@L["Users"]"
                    BreadcrumbItems="@BreadcrumbItems"
                    Toolbar="@Toolbar" />
    </CardHeader>
    <CardBody class="row">

        <Column ColumnSize="ColumnSize.Is8">
        </Column>
        <Column ColumnSize="ColumnSize.Is4" class="form-group row" style="text-align:right;">
            <label for="inputPassword" class="col-sm-4 col-form-label pt-1">  @L["Search"] </label>
            <div class="col-sm-8">
                <TextEdit class="form-control-sm" id="inputPassword" Text="@GetListInput.Filter" TextChanged="@OnSearchTextChanged" />
            </div>
        </Column>

        @* ************************* DATA GRID ************************* *@
        <AbpExtensibleDataGrid TItem="IdentityUserDto"
                               Data="Entities"
                               ReadData="OnDataGridReadAsync"
                               TotalItems="TotalCount"
                               ShowPager="true"
                               PageSize="PageSize"
                               CurrentPage="@CurrentPage"
                               Columns="@UserManagementTableColumns">
        </AbpExtensibleDataGrid>
    </CardBody>
</Card>

@* ************************* CREATE MODAL ************************* *@
@if (HasCreatePermission)
{
    <Modal @ref="CreateModal" Closing="@ClosingCreateModal">
        <ModalContent Centered="true">
            <Form>
                <ModalHeader>
                    <ModalTitle>@L["NewUser"]</ModalTitle>
                    <CloseButton Clicked="CloseCreateModalAsync" />
                </ModalHeader>
                <ModalBody>
                    <Validations @ref="@CreateValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
                        <Tabs @bind-SelectedTab="@CreateModalSelectedTab">
                            <Items>
                                <Tab Name="UserInformations">@L["UserInformations"]</Tab>
                                <Tab Name="Roles">@L["Roles"]</Tab>
                            </Items>
                            <Content>
                                <TabPanel Name="UserInformations">
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:UserName"] *</FieldLabel>
                                            <TextEdit @bind-Text="NewEntity.UserName" Autofocus="true">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Name"]</FieldLabel>
                                            <TextEdit @bind-Text="NewEntity.Name">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Surname"]</FieldLabel>
                                            <TextEdit @bind-Text="NewEntity.Surname">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Password"] *</FieldLabel>
                                            <Addons>
                                                <Addon AddonType="AddonType.Body">
                                                    <TextEdit Role="@_passwordTextRole" @bind-Text="NewEntity.Password">
                                                    </TextEdit>
                                                </Addon>
                                                <Addon AddonType="AddonType.End">
                                                    <Button Color="Color.Secondary" Clicked="@(() => ChangePasswordTextRole(null))">
                                                        <Icon Name="ShowPassword ? IconName.Eye : IconName.EyeSlash" />
                                                    </Button>
                                                </Addon>
                                            </Addons>
                                            <ValidationError Style="display: block" />
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Email"] *</FieldLabel>
                                            <TextEdit @bind-Text="NewEntity.Email">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:PhoneNumber"]</FieldLabel>
                                            <TextEdit @bind-Text="NewEntity.PhoneNumber">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Field>
                                        <Check TValue="bool" @bind-Checked="@NewEntity.IsActive">@L["DisplayName:IsActive"]</Check>
                                    </Field>
                                    <Field>
                                        <Tooltip Text="@L["Description:LockoutEnabled"].Value" style="width: fit-content;" Placement="TooltipPlacement.Right" >
                                            <Check TValue="bool" @bind-Checked="@NewEntity.LockoutEnabled">@L["DisplayName:LockoutEnabled"] <Icon Name="IconName.InfoCircle"/> </Check>
                                        </Tooltip>
                                    </Field>
                                    <ExtensionProperties TEntityType="IdentityUserCreateDto" TResourceType="IdentityResource" Entity="@NewEntity" LH="@LH" ModalType="ExtensionPropertyModalType.CreateModal" />
                                </TabPanel>
                                <TabPanel Name="Roles">
                                    @if (NewUserRoles != null)
                                    {
                                        @foreach (var role in NewUserRoles)
                                        {
                                            <Field>
                                                <input type="hidden" @bind-value="@role.Name" />
                                                <Check TValue="bool" @bind-Checked="@role.IsAssigned">@role.Name</Check>
                                            </Field>
                                        }
                                    }
                                </TabPanel>
                            </Content>
                        </Tabs>
                    </Validations>
                </ModalBody>
                <ModalFooter>
                    <Button Color="Color.Primary" Outline Clicked="CloseCreateModalAsync">@L["Cancel"]</Button>
                    <SubmitButton Clicked="@CreateEntityAsync" />
                </ModalFooter>
            </Form>
        </ModalContent>
    </Modal>
}

@* ************************* EDIT MODAL ************************* *@
@if (HasUpdatePermission)
{
    <Modal @ref="EditModal" Closing="@ClosingEditModal">
        <ModalContent Centered="true">
            <Form>
                <ModalHeader>
                    <ModalTitle>@L["Edit"]</ModalTitle>
                    <CloseButton Clicked="CloseEditModalAsync" />
                </ModalHeader>
                <ModalBody>
                    <Validations @ref="@EditValidationsRef" Model="@EditingEntity" ValidateOnLoad="false">
                        <input type="hidden" name="ConcurrencyStamp" @bind-value="EditingEntity.ConcurrencyStamp" />

                        <Tabs @bind-SelectedTab="@EditModalSelectedTab">
                            <Items>
                                <Tab Name="UserInformations">@L["UserInformations"]</Tab>
                                @if (EditUserRoles != null && EditUserRoles.Any())
                                {
                                    <Tab Name="Roles">@L["Roles"]</Tab>
                                }
                            </Items>
                            <Content>
                                <TabPanel Name="UserInformations">
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:UserName"] *</FieldLabel>
                                            <TextEdit @bind-Text="EditingEntity.UserName" Autofocus="true">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Name"]</FieldLabel>
                                            <TextEdit @bind-Text="EditingEntity.Name">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Surname"]</FieldLabel>
                                            <TextEdit @bind-Text="EditingEntity.Surname">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Password"]</FieldLabel>
                                            <Addons>
                                                <Addon AddonType="AddonType.Body">
                                                    <TextEdit Role="@_passwordTextRole" @bind-Text="EditingEntity.Password">
                                                    </TextEdit>
                                                </Addon>
                                                <Addon AddonType="AddonType.End">
                                                    <Button Color="Color.Secondary" Clicked="@(() => ChangePasswordTextRole(null))">
                                                        <Icon Name="ShowPassword ? IconName.Eye : IconName.EyeSlash" />
                                                    </Button>
                                                </Addon>
                                            </Addons>
                                            <ValidationError Style="display: block" />
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:Email"] *</FieldLabel>
                                            <TextEdit @bind-Text="EditingEntity.Email">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation MessageLocalizer="@LH.Localize">
                                        <Field>
                                            <FieldLabel>@L["DisplayName:PhoneNumber"]</FieldLabel>
                                            <TextEdit @bind-Text="EditingEntity.PhoneNumber">
                                                <Feedback>
                                                    <ValidationError />
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    @if (!IsEditCurrentUser)
                                    {
                                        <Field>
                                            <Check TValue="bool" @bind-Checked="EditingEntity.IsActive">@L["DisplayName:IsActive"]</Check>
                                        </Field>
                                    }
                                    <Field>
                                            <Tooltip Text="@L["Description:LockoutEnabled"].Value" style="width: fit-content;" Placement="TooltipPlacement.Right" >
                                                <Check TValue="bool" @bind-Checked="EditingEntity.LockoutEnabled">@L["DisplayName:LockoutEnabled"] <Icon Name="IconName.InfoCircle"/> </Check>
                                            </Tooltip>
                                        </Field>
                                    <ExtensionProperties TEntityType="IdentityUserUpdateDto" TResourceType="IdentityResource" Entity="@EditingEntity" LH="@LH" ModalType="ExtensionPropertyModalType.EditModal" />
                                </TabPanel>
                                @if (EditUserRoles != null && EditUserRoles.Any())
                                {
                                    <TabPanel Name="Roles">
                                        @if (EditUserRoles != null)
                                        {
                                            @foreach (var role in EditUserRoles)
                                            {
                                                <Field>
                                                    <input type="hidden" @bind-value="@role.Name" />
                                                    <Check TValue="bool" @bind-Checked="@role.IsAssigned">@role.Name</Check>
                                                </Field>
                                            }
                                        }
                                    </TabPanel>
                                }
                            </Content>
                        </Tabs>
                    </Validations>
                </ModalBody>
                <ModalFooter>
                    <Button Color="Color.Primary" Outline Clicked="CloseEditModalAsync">@L["Cancel"]</Button>
                    <SubmitButton Clicked="@UpdateEntityAsync" />
                </ModalFooter>
            </Form>
        </ModalContent>
    </Modal>
}

@if (HasManagePermissionsPermission)
{
    <PermissionManagementModal @ref="PermissionManagementModal" />
}
